package com.cw1.design1Single;

/**
 * @Author qiuMi
 * @description 单例模式 - 双重检查
 * @Date 2022-09-27 21:22
 **/
public class SingleTest06 {
    public static void main(String[] args) {
        Single6 instance1 = Single6.getInstance();
        Single6 instance2 = Single6.getInstance();
        System.out.println(instance1 == instance2);
    }
}

/**
 * 双重检查
 */
class Single6{
    private static volatile Single6 instance;

    private Single6(){}

    /**
     * 提供一个静态的公有方法,加入双重检查代码,解决线程安全问题，同时解决懒加载问题
     * 同时保证效率,推荐使用,延时加载
     * @return single6
     */
    public static Single6 getInstance(){
        if (instance == null){
            synchronized(Single6.class){
                instance = new Single6();
            }
        }
        return instance;
    }
}
